package ro.pub.acs.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

/**
 * In Android, listele functioneaza conform paradigmei MVC (Model View Controller).
 * 
 * In acest tutorial vom invata sa implementam o lista customizabila cu elemente grafice reciclabile.
 * 
 * 
 * Pentru mai multe detalii:
 * http://pdm.ipworkshop.ro/mediawiki/images/f/fc/PDM2010_curs4.pdf
 * 
 * @author Vlad
 *
 */

public class AdvancedLists extends Activity {
	
	private Button adaugaPersonajButton;
	
	private AdaptorPersonaje adaptorPersonaje = new AdaptorPersonaje(this);
	
	//view-ul listei (paradigma MVC)
	private ListView listView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        adaugaPersonajButton = (Button) findViewById(R.id.adaugaPersonajButton);
        
		//tinem lumina ecranului aprinsa
        adaugaPersonajButton.setKeepScreenOn(true);
        
        adaugaPersonajButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Adaugam un personaj nou in lista
				adaptorPersonaje.adaugaPersonajRandom();
			}
		});
        
        //preluam referinta catre lista grafica
        listView = (ListView)findViewById(R.id.personajeListView);
        
        //setam adaptorul (controllerul) listei
        listView.setAdapter(adaptorPersonaje);

        //setam actiune pe apasare lunga
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
				//stergem personajul selectat
				adaptorPersonaje.stergePersonaj(pos);
				return true;
				/*
				 * If your long-click method returns true then you are telling Android that 
				 * you handled the event and nobody else should know about the long-press. 
				 * If your method returns false, Android will still call other handlers such as 
				 * your onItemClick handler. Long-press events in Android typically do not perform 
				 * the same action as a regular press, so it may be best to return true to stop 
				 * event propagation.
				 */
			}
		});
        
        
    }
}